package com.example.rabbitmqdemo.cacheTask;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 缓存对象
 *
 * @param <K>
 * @param <V>
 */
public class CacheObj<K, V> implements Serializable {
    private static final long serialVersionUID = 1L;

    private final K key;
    private V value;
    /**
     * 对象创建时间,
     */
    private final LocalDateTime creatTime;
    /**
     * 过期时间 秒
     */
    private final int ttl;

    public CacheObj(K key, V value, int ttl) {
        this.key = key;
        this.value = value;
        this.creatTime = LocalDateTime.now();
        this.ttl = ttl;
    }


    /**
     * 判断是否过期
     *
     * @return
     */
    public boolean isExpired() {
        return LocalDateTime.now().minusSeconds(ttl).isAfter(this.creatTime);
    }


    public K getKey() {
        return key;
    }

    public V getValue() {
        return value;
    }

    public void setValue(V value) {
        this.value = value;
    }
}
